//
//  MatchCategoryViewController.m
//  YouthOlympic2014
//
//  Created by xiangpaopao on 14-6-25.
//  Copyright (c) 2014年 mitian. All rights reserved.
//

#import "MatchCategoryViewController.h"
#import "UIView+WhenTappedBlocks.h"
#import "SimpleMatchCell.h"
#import "Match.h"
#import "MatchCategory.h"
#import "GameService.h"
#import "MatchByCategoryViewController.h"
#import "MBProgressHUD.h"
#import "AppDelegate.h"

@interface MatchCategoryViewController ()

@property (nonatomic, strong) NSArray* matchCategories;
@property (nonatomic, strong) NSArray*/*Game*/ games;
@property (nonatomic, strong) Game* curGame;
@property (nonatomic, strong) NSArray*/*SubGame*/ subgames;

@property (nonatomic, strong)IBOutlet UITableView *listView;

@property (nonatomic, strong) IBOutlet UIView *shadeView;
@property (nonatomic, strong) IBOutlet UIView* activityView;
@property (nonatomic, strong) IBOutlet UITableView *infoListView;
@property (nonatomic, strong)IBOutlet UIImageView *iconView;
@property (nonatomic, strong)IBOutlet UILabel *titleLabel;
@property (nonatomic, strong)IBOutlet UILabel *subTitleLabel;
@property (nonatomic, getter = isShowing) BOOL show;


@property(nonatomic, strong)MBProgressHUD *HUD;

@end

@implementation MatchCategoryViewController
@synthesize games,subgames,curGame;

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.matchCategories = [Match defaultMatcheCategories];
    
    self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
    self.HUD.mode = MBProgressHUDModeIndeterminate;
    [self.view addSubview:self.HUD];
    
    curGame = nil;
    
    if ([theApp isAppReachable]) {
        [self updateMainDataAndView];
    }
    [self setupActivityView];
}

- (void)updateMainDataAndView
{
    [self.HUD show:YES];
    
    [[[GameService alloc] init] getAllGames:^(id result, NSError *error) {
        
        [self.HUD hide:YES];
        
        if (nil == error) {
            self.games = result;
            [self.listView reloadData];
            NSLog(@"result is: %@",[AppUtils replaceUnicodeDic:result]);
        }else{
            //TODO
        }
    }];
}

#pragma mark - TableView delegate & datasource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
    if(tableView == self.listView){
        return self.games.count;
    }else if(tableView == self.infoListView){
        //return self.subgames.count + 1;
        return self.subgames.count;
    }
    return 0;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *tableViewCell = nil;
    
    if (tableView == self.listView) {
        SimpleMatchCell *cell = [tableView dequeueReusableCellWithIdentifier:@"matchCategoryCell" forIndexPath:indexPath];
       
        Game* game = self.games[indexPath.row];
        cell.titleLabel.text = game.name;
        cell.subTitleLabel.text = game.subname;
        cell.iconView.image = game.icon;
        //
        tableViewCell = cell;
        
    }else if(tableView == self.infoListView){
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"matchDetailCell" forIndexPath:indexPath];
        //
        SubGame* subgame = self.subgames[indexPath.row];
        cell.textLabel.text = subgame.name;
        //
        tableViewCell = cell;
    }
    
    return tableViewCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.listView) {
        Game* game = self.games[indexPath.row];
        curGame = game;
        self.titleLabel.text = game.name;
        self.subTitleLabel.text = game.subname;
        self.iconView.image = game.icon;
        //
        [[[GameService alloc]init] getAllSubGamesByGameId:game.id completion:^(id result, NSError* error){
            SubGame *all = [[SubGame alloc]init];
            all.id = @"";
            all.name = NSLocalizedString(@"所有", nil);
            //
            NSMutableArray* sg = [[NSMutableArray alloc] initWithObjects:all,nil];
            [sg addObjectsFromArray:result];
            //
            self.subgames = [NSArray arrayWithArray: sg];
            [self.infoListView reloadData];
        }];
        
        [self showActivityView];
        
    }else if(tableView == self.infoListView){
        MatchByCategoryViewController *matchCtrl = [self.storyboard instantiateViewControllerWithIdentifier:@"matchByCatCtrl"];
        //
        NSInteger idx = indexPath.row;
        SubGame* subgame = self.subgames[idx];
        [matchCtrl initWithGame: curGame andSubGameId:subgame.id andDate:[AppUtils getCurYMDDateStrInYogRange]];
        //
        [self.navigationController pushViewController:matchCtrl animated:YES];
        [self hideActivityView];
    }

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


#pragma mark - ActivityView
-(void)setupActivityView
{
    
    self.activityView.frame = CGRectMake(0, ScreenHeight, self.activityView.frame.size.width, self.activityView.frame.size.height);
    
    [[[[UIApplication sharedApplication] delegate] window] addSubview:self.shadeView];
    [[[[UIApplication sharedApplication] delegate] window] addSubview:self.activityView];
    
    
    self.shadeView.hidden = YES;
    [self.shadeView whenTapped:^{
        [self hideActivityView];
    }];
    
}
-(void)showActivityView{
    if (self.isShowing) {
        return;
    }
    
    [UIView animateWithDuration:0.3 animations:^{
        self.shadeView.hidden = NO;
        self.activityView.frame = CGRectMake(0,
                                             ScreenHeight- self.activityView.frame.size.height,
                                             self.activityView.frame.size.width,
                                             self.activityView.frame.size.height);
        
        self.shadeView.alpha = .2;
    } completion:^(BOOL finished) {
        
        self.show = YES;
    }];
}

-(void)hideActivityView{
    if (!self.isShowing) {
        return;
    }
    
    [UIView animateWithDuration:0.3 animations:^{
        self.shadeView.alpha = 0;
        self.shadeView.alpha = 0.0;
        self.shadeView.hidden = YES;
        self.activityView.frame = CGRectMake(0, ScreenHeight, self.activityView.frame.size.width, self.activityView.frame.size.height);
        [self.infoListView reloadData];
    } completion:^(BOOL finished) {
        self.show = NO;
    }];
}
-(IBAction)cancelAction:(id)sender
{
    [self hideActivityView];
}



@end
